<?php

class Zing_Cache_APC implements Zing_Cache_Interface
{
	const EXPIRE_DEFAULT = 2592000; // 30 ngay
	private $_profiler = null;
    private $_profiler_enable = false;
	
	function __construct($profiler_enable = false)
	{
		if($profiler_enable)
    	{
    		$this->_profiler_enable = true;
    		$this->_profiler = new Zing_Cache_ZingCacheProfiler();
    	}
	}
	
	
	public function flushLocalCache()
	{
		return;
	}
	public function getMultiCache($keys)
	{
	    if (SGN_Application::$params['refreshcache']) {
            return false;
        }
	 	$return = array();
	 	
	 	if(is_array($keys) && count($keys) > 0)
	 	{
	 		for($i=0;$i<count($keys);$i++)
	 		{
	 			$key = $keys[$i];
	 			$result = $this->getCache($key);
	 			$return[$key] = $result; 
	 		}
	 	}
		return $return;
	}
	
	public function getCache($key)
	{
	    if (SGN_Application::$params['refreshcache']) {
            return false;
        }
	 	$starttime = gettimeofday(true);
	 	$result = apc_fetch($key);
	 	$endtime = gettimeofday(true);
	 	if($this->_profiler_enable) $this->_profiler->pushToCacheProfiler($key, $result, $starttime, $endtime);
	 	return $result;
	}	
	
	public function deleteCache($key)
	{
	 	apc_delete($key);
	}
	
	public function setCache($key, $value, $expireTime = 0, $compress=0)
	{
	    // store all keyname into configuration.keys list. It's easier to destroy cache
	    $configKeys = $this->getCache('configuration.keys');
	    $configKeys[$key] = $key;
	    apc_store('configuration.keys', $configKeys);
		apc_store($key, $value, $expireTime); 	
	}
	
	public function getProfilerData($cache_name = '')
	{
		$output = "";
    	if($this->_profiler_enable)
    	{
    		$cache_name .= " " . $this->_server . ":" . $this->_port;
    		$output = $this->_profiler->getProfilerData($cache_name . " (APC)");
    	}
		$data_profile = apc_cache_info();		
		$output .= "total items : " . $data_profile['num_entries'] . "<br>";
		//$output .= "memsize : " . $data_profile['mem_size'] . "<br>";
		$data_profile = apc_sma_info();
		$output .= "mem available : " . $data_profile['avail_mem'] . "<br>";
		
    	return $output; 	
	}
}



